package com.lindroid.androiduilearning.util.font;

import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author Lin
 * @date 2018/3/11
 * @function 改变字体工具类
 */

public class FontChangeUtils {
    private final static String CUSTOM_FONT = "fonts/precious.ttf";

    public static void replaceCustomFont(View rootView) {
        if (rootView == null) {
            return;
        }
        if (rootView instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) rootView;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                //遍历出子类，再判断是不是TextView
                replaceCustomFont(viewGroup.getChildAt(i));
            }
        } else if (rootView instanceof TextView) {
            TextView textView = (TextView) rootView;
            int fontStyle = Typeface.NORMAL;
            if (textView.getTypeface() != null) {
                fontStyle = textView.getTypeface().getStyle();
            }
            Typeface customFont = Typeface.createFromAsset(rootView.getContext().getAssets(), CUSTOM_FONT);
            textView.setTypeface(customFont, fontStyle);
        }
    }
}
